<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品管理API测试</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }
        
        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
        }
        
        .header p {
            font-size: 1.1em;
            opacity: 0.9;
        }
        
        .content {
            padding: 30px;
        }
        
        .section {
            margin-bottom: 40px;
            padding: 25px;
            border: 1px solid #e1e5e9;
            border-radius: 10px;
            background: #f8f9fa;
        }
        
        .section h2 {
            color: #333;
            margin-bottom: 20px;
            font-size: 1.5em;
            border-bottom: 2px solid #667eea;
            padding-bottom: 10px;
        }
        
        .form-group {
            margin-bottom: 15px;
        }
        
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: 600;
            color: #555;
        }
        
        input, textarea, select {
            width: 100%;
            padding: 12px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.3s ease;
        }
        
        input:focus, textarea:focus, select:focus {
            outline: none;
            border-color: #667eea;
        }
        
        .btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 12px 25px;
            border-radius: 8px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 600;
            transition: transform 0.2s ease;
            margin-right: 10px;
            margin-bottom: 10px;
        }
        
        .btn:hover {
            transform: translateY(-2px);
        }
        
        .btn-secondary {
            background: #6c757d;
        }
        
        .btn-success {
            background: #28a745;
        }
        
        .btn-danger {
            background: #dc3545;
        }
        
        .btn-warning {
            background: #ffc107;
            color: #333;
        }
        
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 8px;
            font-family: 'Courier New', monospace;
            font-size: 12px;
            white-space: pre-wrap;
            max-height: 300px;
            overflow-y: auto;
        }
        
        .result.success {
            background: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        
        .result.error {
            background: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        
        .grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }
        
        .product-card {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            transition: transform 0.2s ease;
        }
        
        .product-card:hover {
            transform: translateY(-5px);
        }
        
        .product-card img {
            width: 100%;
            height: 200px;
            object-fit: cover;
            border-radius: 8px;
            margin-bottom: 15px;
        }
        
        .product-card h3 {
            color: #333;
            margin-bottom: 10px;
        }
        
        .product-card p {
            color: #666;
            margin-bottom: 10px;
        }
        
        .product-card .price {
            font-size: 1.2em;
            font-weight: bold;
            color: #667eea;
        }
        
        .product-card .stock {
            color: #28a745;
            font-weight: 600;
        }
        
        .loading {
            text-align: center;
            padding: 20px;
            color: #666;
        }
        
        .spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #667eea;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            animation: spin 1s linear infinite;
            margin: 0 auto 10px;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🛍️ 商品管理API测试</h1>
            <p>测试商品管理系统的REST API接口</p>
        </div>
        
        <div class="content">
            <!-- 添加商品 -->
            <div class="section">
                <h2>➕ 添加商品</h2>
                <form id="addProductForm">
                    <div class="form-group">
                        <label for="productName">商品名称 *</label>
                        <input type="text" id="productName" required>
                    </div>
                    <div class="form-group">
                        <label for="productDescription">商品描述</label>
                        <textarea id="productDescription" rows="3"></textarea>
                    </div>
                    <div class="form-group">
                        <label for="productPrice">价格 *</label>
                        <input type="number" id="productPrice" step="0.01" min="0.01" required>
                    </div>
                    <div class="form-group">
                        <label for="productStock">库存 *</label>
                        <input type="number" id="productStock" min="0" required>
                    </div>
                    <div class="form-group">
                        <label for="productCategory">分类</label>
                        <select id="productCategory">
                            <option value="">选择分类</option>
                            <option value="手机数码">手机数码</option>
                            <option value="电脑办公">电脑办公</option>
                            <option value="智能穿戴">智能穿戴</option>
                            <option value="智能家居">智能家居</option>
                            <option value="配件周边">配件周边</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="productImageUrl">图片URL</label>
                        <input type="url" id="productImageUrl" placeholder="https://example.com/image.jpg">
                    </div>
                    <button type="submit" class="btn btn-success">添加商品</button>
                </form>
                <div id="addResult" class="result" style="display: none;"></div>
            </div>
            
            <!-- 商品操作 -->
            <div class="section">
                <h2>🔧 商品操作</h2>
                <button class="btn" onclick="getAllProducts()">获取所有商品</button>
                <button class="btn btn-secondary" onclick="getProductsByCategory('手机数码')">获取手机数码</button>
                <button class="btn btn-warning" onclick="searchProducts('iPhone')">搜索iPhone</button>
                <button class="btn btn-danger" onclick="getLowStockProducts(20)">库存不足商品</button>
                <button class="btn" onclick="getCategories()">获取分类</button>
                <button class="btn" onclick="getCategoryStats()">分类统计</button>
                <div id="operationResult" class="result" style="display: none;"></div>
            </div>
            
            <!-- 商品列表 -->
            <div class="section">
                <h2>📦 商品列表</h2>
                <div id="productsGrid" class="grid">
                    <div class="loading">
                        <div class="spinner"></div>
                        <p>加载中...</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        const API_BASE_URL = 'http://localhost:8080/api/products';
        
        // 显示结果
        function showResult(elementId, data, isError = false) {
            const element = document.getElementById(elementId);
            element.textContent = JSON.stringify(data, null, 2);
            element.className = `result ${isError ? 'error' : 'success'}`;
            element.style.display = 'block';
        }
        
        // 显示加载状态
        function showLoading(elementId) {
            const element = document.getElementById(elementId);
            element.innerHTML = '<div class="spinner"></div><p>请求中...</p>';
            element.style.display = 'block';
        }
        
        // 隐藏结果
        function hideResult(elementId) {
            document.getElementById(elementId).style.display = 'none';
        }
        
        // 添加商品
        document.getElementById('addProductForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const formData = {
                name: document.getElementById('productName').value,
                description: document.getElementById('productDescription').value,
                price: parseFloat(document.getElementById('productPrice').value),
                stock: parseInt(document.getElementById('productStock').value),
                category: document.getElementById('productCategory').value,
                imageUrl: document.getElementById('productImageUrl').value
            };
            
            try {
                showLoading('addResult');
                
                const response = await fetch(API_BASE_URL, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(formData)
                });
                
                const result = await response.json();
                
                if (result.success) {
                    showResult('addResult', result);
                    document.getElementById('addProductForm').reset();
                    getAllProducts(); // 刷新商品列表
                } else {
                    showResult('addResult', result, true);
                }
            } catch (error) {
                showResult('addResult', { error: error.message }, true);
            }
        });
        
        // 获取所有商品
        async function getAllProducts() {
            try {
                showLoading('operationResult');
                
                const response = await fetch(API_BASE_URL);
                const result = await response.json();
                
                if (result.success) {
                    showResult('operationResult', result);
                    displayProducts(result.data);
                } else {
                    showResult('operationResult', result, true);
                }
            } catch (error) {
                showResult('operationResult', { error: error.message }, true);
            }
        }
        
        // 根据分类获取商品
        async function getProductsByCategory(category) {
            try {
                showLoading('operationResult');
                
                const response = await fetch(`${API_BASE_URL}/category/${encodeURIComponent(category)}`);
                const result = await response.json();
                
                if (result.success) {
                    showResult('operationResult', result);
                    displayProducts(result.data);
                } else {
                    showResult('operationResult', result, true);
                }
            } catch (error) {
                showResult('operationResult', { error: error.message }, true);
            }
        }
        
        // 搜索商品
        async function searchProducts(name) {
            try {
                showLoading('operationResult');
                
                const response = await fetch(`${API_BASE_URL}/search?name=${encodeURIComponent(name)}`);
                const result = await response.json();
                
                if (result.success) {
                    showResult('operationResult', result);
                    displayProducts(result.data);
                } else {
                    showResult('operationResult', result, true);
                }
            } catch (error) {
                showResult('operationResult', { error: error.message }, true);
            }
        }
        
        // 获取库存不足商品
        async function getLowStockProducts(threshold) {
            try {
                showLoading('operationResult');
                
                const response = await fetch(`${API_BASE_URL}/low-stock?threshold=${threshold}`);
                const result = await response.json();
                
                if (result.success) {
                    showResult('operationResult', result);
                    displayProducts(result.data);
                } else {
                    showResult('operationResult', result, true);
                }
            } catch (error) {
                showResult('operationResult', { error: error.message }, true);
            }
        }
        
        // 获取分类
        async function getCategories() {
            try {
                showLoading('operationResult');
                
                const response = await fetch(`${API_BASE_URL}/categories`);
                const result = await response.json();
                
                showResult('operationResult', result);
            } catch (error) {
                showResult('operationResult', { error: error.message }, true);
            }
        }
        
        // 获取分类统计
        async function getCategoryStats() {
            try {
                showLoading('operationResult');
                
                const response = await fetch(`${API_BASE_URL}/categories/stats`);
                const result = await response.json();
                
                showResult('operationResult', result);
            } catch (error) {
                showResult('operationResult', { error: error.message }, true);
            }
        }
        
        // 显示商品列表
        function displayProducts(products) {
            const grid = document.getElementById('productsGrid');
            
            if (!products || products.length === 0) {
                grid.innerHTML = '<p style="text-align: center; color: #666; grid-column: 1 / -1;">暂无商品数据</p>';
                return;
            }
            
            grid.innerHTML = products.map(product => `
                <div class="product-card">
                    <img src="${product.imageUrl || 'https://via.placeholder.com/300x200?text=No+Image'}" 
                         alt="${product.name}" 
                         onerror="this.src='https://via.placeholder.com/300x200?text=Image+Error'">
                    <h3>${product.name}</h3>
                    <p>${product.description || '暂无描述'}</p>
                    <p class="price">¥${product.price}</p>
                    <p class="stock">库存: ${product.stock}</p>
                    <p>分类: ${product.category || '未分类'}</p>
                    <p>状态: ${product.isActive ? '激活' : '禁用'}</p>
                </div>
            `).join('');
        }
        
        // 页面加载时获取商品列表
        window.addEventListener('load', getAllProducts);
    </script>
</body>
</html> 